package com.jushu.storbox.service;

import java.util.List;

import android.content.Context;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.database.DAOFactory;
import com.jushu.storbox.database.ITeamSpaceMemberDao;
import com.jushu.storbox.entities.TeamSpaceMemberInfo;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.request.TeamSpaceAddMemberRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.TeamSpaceMemberListRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.TeamSpaceMemberMessageRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.UpdataTeamSpaceMemberRequest;
import com.huawei.sharedrive.sdk.android.modelV2.response.TeamSpaceMemberListResourse;
import com.huawei.sharedrive.sdk.android.modelV2.response.TeamSpaceMemberShipsEntity;
import com.huawei.sharedrive.sdk.android.serviceV2.TeamClient;

public class TeamSpaceMemberService implements ITeamSpaceMemberService{

	@Override
	public TeamSpaceMemberInfo getTeamSpaceMemberMessageFromServer(
			Context context, TeamSpaceMemberMessageRequest request) throws ClientException {
		// TODO Auto-generated method stub
		TeamSpaceMemberShipsEntity teamSpaceMemberShipsEntity=TeamClient.getInstance().getTeamSpaceMemberMessage(ShareDriveApplication.getInstance().getAuthorization(), request);
		ITeamSpaceMemberDao teamSpaceMemberDao=DAOFactory.instance(context).getTeamSpaceMemberDao();
		if(null ==teamSpaceMemberDao.getTeamSpaceMember(teamSpaceMemberShipsEntity.getTeamspace().getId(),teamSpaceMemberShipsEntity.getId())){
			teamSpaceMemberDao.insertTeamSpaceMember(teamSpaceMemberShipsEntity);
		}else{
		teamSpaceMemberDao.updataTeamSpaceMember(teamSpaceMemberShipsEntity);
		}
		return teamSpaceMemberDao.getTeamSpaceMember(teamSpaceMemberShipsEntity.getTeamspace().getId(),teamSpaceMemberShipsEntity.getId());
	}

	@Override
	public TeamSpaceMemberInfo getTeamSpaceMemberMessageFromLocal(
			Context context, TeamSpaceMemberMessageRequest request) {
		// TODO Auto-generated method stub
		ITeamSpaceMemberDao teamSpaceMemberDao=DAOFactory.instance(context).getTeamSpaceMemberDao();
		return teamSpaceMemberDao.getTeamSpaceMember(request.getTeamId(),request.getId());
	}

	@Override
	public TeamSpaceMemberInfo updateTeamSpaceMemberMessageFromServer(
			Context context, UpdataTeamSpaceMemberRequest request) throws ClientException {
		TeamSpaceMemberShipsEntity teamSpaceMemberShipsEntity=TeamClient.getInstance().updataTeamSpaceMemberMessage(ShareDriveApplication.getInstance().getAuthorization(), request);
		ITeamSpaceMemberDao teamSpaceMemberDao=DAOFactory.instance(context).getTeamSpaceMemberDao();
		teamSpaceMemberDao.updataTeamSpaceMember(teamSpaceMemberShipsEntity);
		return teamSpaceMemberDao.getTeamSpaceMember(teamSpaceMemberShipsEntity.getTeamspace().getId(),teamSpaceMemberShipsEntity.getId());
	}

	@Override
	public List<TeamSpaceMemberInfo> getTeamSpaceMemberListFromServer(
			Context context, TeamSpaceMemberListRequest request) throws ClientException {
		// TODO Auto-generated method stub
		TeamSpaceMemberListResourse resourse=TeamClient.getInstance().getTeamSpaceMemberList(ShareDriveApplication.getInstance().getAuthorization(), request);
		ITeamSpaceMemberDao teamSpaceMemberDao=DAOFactory.instance(context).getTeamSpaceMemberDao();
		List<TeamSpaceMemberInfo> clientList=teamSpaceMemberDao.getTeamSpaceMemberList(request.getTeamId());
		TeamSpaceHelper.compareServiceClientTeamSpaceMember(context, resourse.getMemberships(), clientList);
		return teamSpaceMemberDao.getTeamSpaceMemberList(request.getTeamId());
	}

	@Override
	public List<TeamSpaceMemberInfo> getTeamSpaceMemberListFromLocal(
			Context context, String teamSpaceId) {
		// TODO Auto-generated method stub
		ITeamSpaceMemberDao teamSpaceMemberDao=DAOFactory.instance(context).getTeamSpaceMemberDao();
		return teamSpaceMemberDao.getTeamSpaceMemberList(teamSpaceId);
	}

	@Override
	public String deleteTeamSpaceMemberFromServer(Context context,
			TeamSpaceMemberMessageRequest request) throws ClientException {
		// TODO Auto-generated method stub
		String res=null;
		res=TeamClient.getInstance().deleteTeamSpaceMember(ShareDriveApplication.getInstance().getAuthorization(), request);
		ITeamSpaceMemberDao teamSpaceMemberDao=DAOFactory.instance(context).getTeamSpaceMemberDao();
		teamSpaceMemberDao.deleteTeamSpaceMember(request.getTeamId(),request.getId());
		return res;
	}

	@Override
	public TeamSpaceMemberInfo addTeamSpaceMemberFromServer(Context context,
			TeamSpaceAddMemberRequest request) throws ClientException {
		TeamSpaceMemberShipsEntity teamSpaceMemberShipsEntity=TeamClient.getInstance().addTeamSpaceMember(ShareDriveApplication.getInstance().getAuthorization(), request);
		ITeamSpaceMemberDao teamSpaceMemberDao=DAOFactory.instance(context).getTeamSpaceMemberDao();
		teamSpaceMemberDao.insertTeamSpaceMember(teamSpaceMemberShipsEntity);
		return teamSpaceMemberDao.getTeamSpaceMember(teamSpaceMemberShipsEntity.getTeamspace().getId(),teamSpaceMemberShipsEntity.getId());
	}

	

}
